import {
  Controller,
  Get,
  Body,
  Patch,
  Param,
  Delete,
  ValidationPipe,
  Post,
  UseGuards,
} from '@nestjs/common';
import { UserService } from './user.service';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { HttpReslut } from 'src/common/result/http-reslut';
import { User } from 'src/entities/user.entity';
import { SearchDto } from './dto/search.dto';
import { JwtAuthGuard } from 'src/common/jwt/JwtAuthGuard';
import { UpdateUser } from './dto/updateUser.dto';

@ApiBearerAuth()
@UseGuards(new JwtAuthGuard())
@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @ApiOperation({
    summary: '查询所有用户',
  })
  @ApiBody({
    type: SearchDto,
  })
  @Post()
  async findAll(@Body('search') search: string): Promise<HttpReslut<User[]>> {
    const userList: User[] = await this.userService.findAll(search);
    return new HttpReslut<User[]>(userList);
  }

  @ApiOperation({
    summary: '查询用户详情',
  })
  @Get(':id')
  async findOne(@Param('id') id: string): Promise<HttpReslut<User>> {
    const user = await this.userService.findOne(id);
    return new HttpReslut<User>(user);
  }

  @ApiOperation({
    summary: '修改用户',
  })
  @ApiBody({
    type: UpdateUser,
  })
  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body(new ValidationPipe()) user: UpdateUser,
  ): Promise<HttpReslut<boolean>> {
    await this.userService.update(id, user);
    return new HttpReslut<boolean>(true);
  }

  @ApiOperation({
    summary: '删除用户',
  })
  @Delete(':id')
  async remove(@Param('id') id: string): Promise<HttpReslut<boolean>> {
    await this.userService.remove(id);
    return new HttpReslut<boolean>(true);
  }
}
